/*
 * frmChangePassword.java
 *
 * Created on April 28, 2010, 5:30 PM
 */

package GUI;

import Validate_Connect.CheckForm;
import Validate_Connect.DataInteraction;
import Interact.Login;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Shinichi
 */
public class frmChangePassword extends javax.swing.JDialog {

    /** Creates new form frmChangePassword */
    public frmChangePassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();        
        _lblusernamehere.setText(Login.get_Username());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        _lblusernamehere = new javax.swing.JLabel();
        _pwfoldpass = new javax.swing.JPasswordField();
        _pwfnewpass = new javax.swing.JPasswordField();
        _pwfconfirmpass = new javax.swing.JPasswordField();
        _btnchangepass = new javax.swing.JButton();
        _btncancel = new javax.swing.JButton();
        _lblerr = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Change Password"));

        jLabel2.setText("Username:");

        jLabel3.setText("Old Password:");

        jLabel4.setText("New Password:");

        jLabel5.setText("Confirm Password:");

        _lblusernamehere.setFont(new java.awt.Font("Tahoma", 1, 11));
        _lblusernamehere.setForeground(new java.awt.Color(255, 51, 0));
        _lblusernamehere.setText("_usernamehere");

        _btnchangepass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Settings1.png"))); // NOI18N
        _btnchangepass.setText("Change Password");
        _btnchangepass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnchangepassActionPerformed(evt);
            }
        });

        _btncancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/window_close2.png"))); // NOI18N
        _btncancel.setText("Cancel");
        _btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btncancelActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .add(_lblerr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 279, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(254, 254, 254))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(137, Short.MAX_VALUE)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jLabel5)
                    .add(jLabel4)
                    .add(jLabel3))
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(36, 36, 36)
                        .add(_lblusernamehere))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(26, 26, 26)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, _pwfnewpass)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, _pwfconfirmpass)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, _pwfoldpass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 219, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .add(111, 111, 111))
            .add(jPanel2Layout.createSequentialGroup()
                .add(186, 186, 186)
                .add(_btnchangepass)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(_btncancel)
                .addContainerGap(167, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(_lblerr)
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(_lblusernamehere)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(_pwfoldpass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(_pwfnewpass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(_pwfconfirmpass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .add(18, 18, 18)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(_btnchangepass)
                    .add(_btncancel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/password.png"))); // NOI18N
        jLabel1.setText("Change Password:");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jLabel1)
                .addContainerGap(267, Short.MAX_VALUE))
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void _btnchangepassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnchangepassActionPerformed
try {
            char[] _chroldpass = _pwfoldpass.getPassword();
            char[] _chrnewpass = _pwfnewpass.getPassword();
            char[] _chrconfirmpass = _pwfconfirmpass.getPassword();
            String _oldpass = new String(_chroldpass);
            String _newpass = new String(_chrnewpass);
            String _confirmpass = new String(_chrconfirmpass);
            if (!CheckForm .isPwdUsername(_oldpass)) {
                JOptionPane.showMessageDialog(this, "Please enter old password","Warning",JOptionPane.WARNING_MESSAGE);
                _pwfoldpass.requestFocus();
                return;
            }
            if (!CheckForm.isPwdUsername(_newpass)) {
                JOptionPane.showMessageDialog(this, "Please enter new password","Warning",JOptionPane.WARNING_MESSAGE);
                _pwfnewpass.requestFocus();
                return;
            }
            if (!CheckForm.isPwdUsername(_confirmpass)) {
                JOptionPane.showMessageDialog(this, "Please enter confirm password","Warning",JOptionPane.WARNING_MESSAGE);
                _pwfconfirmpass.requestFocus();
                return;
            }
            if (!_newpass.equals(_confirmpass)) {
                JOptionPane.showMessageDialog(this, "Confirm new password incorrectly.","Warning",JOptionPane.WARNING_MESSAGE);
                return;
            }
            String sql = "select * from tblUser where UserID='" + Interact.Login.get_UserID() + "'";
            ResultSet rs = DataInteraction.queryResultSet(sql);
            String dataPass = null;
            while (rs.next()) {
                dataPass = rs.getString("Password");
            }
            if (!_oldpass.equals(dataPass)) {
                JOptionPane.showMessageDialog(this, "Old password incorrectly.","Warning",JOptionPane.WARNING_MESSAGE);
                return;
            } else {
                DataInteraction.exec("update tblUser set Password='" + _newpass + "' where UserID='" + Interact.Login.get_UserID() + "'");
                JOptionPane.showMessageDialog(this, "Change password successfully.");
                this.dispose();
            }
        } catch (SQLException ex) {
            Logger.getLogger(frmChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event__btnchangepassActionPerformed

private void _btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btncancelActionPerformed
this.dispose();
}//GEN-LAST:event__btncancelActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                frmChangePassword dialog = new frmChangePassword(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _btncancel;
    private javax.swing.JButton _btnchangepass;
    private javax.swing.JLabel _lblerr;
    private javax.swing.JLabel _lblusernamehere;
    private javax.swing.JPasswordField _pwfconfirmpass;
    private javax.swing.JPasswordField _pwfnewpass;
    private javax.swing.JPasswordField _pwfoldpass;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables

}
